package zcj.stu.controller;

import org.springframework.web.bind.annotation.*;
import zcj.stu.Entity.Account;
import zcj.stu.service.AccountService;
import zcj.stu.service.CommonService;
import zcj.stu.utils.Result;

import javax.annotation.Resource;


@RestController
@RequestMapping("account")
public class AccountController {
    @Resource
    private AccountService accountService;
    @Resource
    private CommonService commonService;

    @PutMapping("/edit")
    public Result edit(@RequestBody Account account) {
        try {
            boolean isSuccess = accountService.edit(account);
            if (isSuccess) {
                return Result.ok();
            } else {
                return Result.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @DeleteMapping ("/del/{_id}")
    public Result del(@PathVariable("_id") String _id) {
        try {
            boolean isSuccess = accountService.del(_id);
            if (isSuccess) {
                return Result.ok();
            } else {
                return Result.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @PostMapping("/save")
    public Result save(@RequestBody Account account) {
        try {
            boolean success = accountService.save(account);
            if (success) {
                return Result.ok();
            } else {
                return Result.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @GetMapping("/getAccount")
    public Result getAccountPage(int currentPage, int pageSize) {
        try {
            return commonService.getAccountPage(currentPage, pageSize,Account.class, null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @PostMapping("/ckLogin")
    public Result loginCk(@RequestBody Account account) {
        try {
            Account ckFlag = accountService.ckLogin(account);
            if (ckFlag != null) {
                return Result.ok(ckFlag);
            } else {
                return Result.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
}
